<template>
  <div>
      <el-form :inline="true" v-show="isSearchCollapse" class="query-form" ref="searchForm" :model="searchForm" @keyup.enter.native="refreshList()" @submit.native.prevent>
        <el-form-item prop="abnormalArea">
               <el-input size="small" v-model="searchForm.abnormalArea" placeholder="异常区域" clearable></el-input>
        </el-form-item>
        <el-form-item prop="abnormalObject">
               <el-input size="small" v-model="searchForm.abnormalObject" placeholder="异常对象" clearable></el-input>
        </el-form-item>
        <el-form-item prop="code">
               <el-input size="small" v-model="searchForm.code" placeholder="设备编码" clearable></el-input>
        </el-form-item>
        <el-form-item prop="abnormalMessage">
               <el-input size="small" v-model="searchForm.abnormalMessage" placeholder="异常信息" clearable></el-input>
        </el-form-item>
        <el-form-item prop="abnormalLevel">
               <el-input size="small" v-model="searchForm.abnormalLevel" placeholder="异常级别" clearable></el-input>
        </el-form-item>
        <el-form-item prop="firstUser">
               <el-input size="small" v-model="searchForm.firstUser" placeholder="第一责任人" clearable></el-input>
        </el-form-item>
        <el-form-item prop="createDate">
               <el-date-picker
                 v-model="searchForm.createDate"
                 type="datetime"
                 size="small"
                 value-format="yyyy-MM-dd HH:mm:ss"
                 placeholder="选择日期时间">
               </el-date-picker>
        </el-form-item>
        <el-form-item label="创建时间" prop="searchDates">
          <el-date-picker
            v-model="searchDates"
            type="daterange"
            size="small"
            align="right"
            value-format="yyyy-MM-dd hh:mm:ss"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button  type="primary" @click="refreshList()" size="small">查询</el-button>
          <el-button @click="resetSearch()" size="small">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row>
        <!-- <el-button  type="primary" size="small"  @click="inpPutLibrary(row)">发起异常</el-button> -->
        <el-button-group class="pull-right">
          <el-tooltip class="item" effect="dark" content="搜索" placement="top">
            <el-button
              type="default"
              size="small"
              icon="el-icon-search"
              @click="isSearchCollapse = !isSearchCollapse, isImportCollapse=false">
            </el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="刷新" placement="top">
            <el-button
              type="default"
              size="small"
              icon="el-icon-refresh"
              @click="refreshList">
            </el-button>
          </el-tooltip>
        </el-button-group>
      </el-row>
        <el-table
          :data="dataList"
          :row-class-name="tableRowClassName"
          border
          size = "medium"
          v-loading="loading"
          @selection-change="selectionChangeHandle"
          class="table">
          <el-table-column
            type="selection"
            header-align="center"
            align="center"
            width="50">
          </el-table-column>
          <!-- <el-table-column
              prop="task.code"
              header-align="center"
              align="center"
              show-overflow-tooltip
              sortable="custom"
              label="异常区域">
            </el-table-column> -->
          <el-table-column
              prop="task.checkStandardAbnormal.code"
              header-align="center"
              align="center"
              show-overflow-tooltip
              sortable="custom"
              label="设备编码">
            </el-table-column>
          <el-table-column
              prop="task.checkStandardAbnormal.abnormalArea"
              header-align="center"
              align="center"
              show-overflow-tooltip
              sortable="custom"
              label="异常区域">
            </el-table-column>
          <el-table-column
              prop="task.checkStandardAbnormal.factory"
              header-align="center"
              align="center"
              show-overflow-tooltip
              sortable="custom"
              label="异常设备">
            </el-table-column>
          <!-- <el-table-column
              prop="task.checkStandardAbnormal.abnormalMessgae"
              header-align="center"
              align="center"
              show-overflow-tooltip
              sortable="custom"
              label="异常信息">
            </el-table-column> -->
          <el-table-column
              prop="task.checkStandardAbnormal.firstUser.name"
              header-align="center"
              align="center"
              show-overflow-tooltip
              sortable="custom"
              label="第一责任人">
            </el-table-column>
          <el-table-column
            prop="vars.title"
            label="实例标题">
                <template slot-scope="scope">
                  <el-link  type="primary" :underline="false" v-if="scope.row.status === 'todo'"  @click="todo(scope.row)">{{scope.row.vars.title}}</el-link>
                  <span v-else>{{scope.row.vars.title}}</span>
                </template>
          </el-table-column>
          <el-table-column
            prop="procDef.name"
            label="流程名称">
          </el-table-column>
           <el-table-column
            prop="task.name"
            label="当前环节">
              <template slot-scope="scope">
                <el-tag>{{scope.row.task.name}}</el-tag>
             </template>
          </el-table-column>
           <el-table-column
            prop="vars.userName"
            label="流程发起人">
          </el-table-column>
          <el-table-column
            prop="task.createTime"
            show-overflow-tooltip
            label="创建时间">
             <template slot-scope="scope">
              {{scope.row.task.createTime | formatDate}}
             </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            width="200"
            label="操作">
            <template slot-scope="scope">
              <el-button v-if="scope.row.status === 'claim'" type="text" size="small" @click="claim(scope.row)">签收任务</el-button>
              <el-button v-if="scope.row.status === 'todo'" type="text" size="small" @click="transferTask(scope.row)">委派</el-button>
              <el-button v-if="scope.row.status === 'todo'" type="text" size="small" @click="todo(scope.row)">办理</el-button>
              <el-button v-if="scope.row.claimTime" type="text" size="small" @click="unclaim(scope.row)">取消签收</el-button>
              <el-button  type="text" size="small" @click="trace(scope.row)">进度</el-button>
            </template>
          </el-table-column>
        </el-table>
       <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageNo"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="total"
        background
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
       <el-dialog
        title="查看进度"
        :close-on-click-modal="true"
        :visible.sync="visible"
         v-dialogDrag
        width="70%"
        height="600px">
          <flow-chart ref="preview" :processInstanceId="processInstanceId"></flow-chart>
        </el-dialog>
        <user-select ref="userSelect" :limit="1" @doSubmit="selectUsersToTransferTask"></user-select>
        <!-- 弹窗, 新增 / 修改 -->
        <ProcessList ref="ProcessList" v-show='false'></ProcessList>
        <CheckInclinationForm  ref="checkInclinationForm" @refreshDataList="refreshList"></CheckInclinationForm>
  </div>
</template>

<script>
  // import FlowChart from '../modeler/FlowChart'
  import CheckInclinationForm from '../checkinclination/CheckInclinationForm.vue'
  import pick from 'lodash.pick'
  import ProcessList from './ProcessList.vue'
  import UserSelect from '@/components/userSelect/UserSelectDialog'
  export default {
    data () {
      return {
        searchForm: {
          beginDate: '',
          endDate: '',
          abnormalArea: '',
          abnormalObject: '',
          code: '',
          abnormalMessgae: '',
          abnormalLevel: '',
          firstUser: '',
          handler: '',
          createBy: {
            id: ''
          },
          createDate: '',
          remarks: ''
        },
        task: {
          checkStandardAbnormal: {
            code: '',
            factory: '',
            abnormalArea: '',
            abnormalObject: '',
            abnormalMessgae: '',
            abnormalLevel: '',
            firstUser: {
              name: ''
            }
          }
        },
        searchDates: '',
        dataList: [],
        row: {
          id: "Process_1605429711774:4:4088865e-27cd-11eb-9113-00ff0962600f",
          key: "Process_1605429711774",
          name: "异常流程"
        },
        pageNo: 1,
        pageSize: 10,
        total: 0,
        isSearchCollapse: false,
        loading: false,
        visible: false,
        currentTask: null,
        dataListSelections: [],
        processInstanceId: '',
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick (picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近一个月',
            onClick (picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近三个月',
            onClick (picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }]
        }
      }
    },
    activated () {
      this.refreshList()
    },
    components: {
      UserSelect,
      CheckInclinationForm,
      ProcessList
      // FlowChart
    },
    watch: {
      searchDates () {
        if (this.searchDates) {
          this.searchForm.beginDate = this.searchDates[0]
          this.searchForm.endDate = this.searchDates[1]
        } else {
          this.searchForm.beginDate = ''
          this.searchForm.endDate = ''
        }
      }
    },
    methods: {
      //当前环节带有异常则显示
      // tableRowClassName({row, rowIndex}) {
      //   var result = row.task.name.indexOf("异常");
      //   if (result === 0) {
      //             return 'yesshow'
      //           } else if (result === -1) {
      //             return 'noshow';
      //           }
      // },
      // 获取数据列表
      refreshList () {
        this.loading = true
        this.$http({
          url: '/flowable/task/todoAbnormalChick',
          method: 'get',
          params: {
            'pageNo': this.pageNo,
            'pageSize': this.pageSize,
            ...this.searchForm
          }
        }).then(({data}) => {
          if (data && data.success) {
            // this.pageSize = data.page.count
            // console.log(data)
            // var myArray = new Array(data.page.list.length)
            // for (let varr in data.page.list) {
            //   console.log(data.page.list[varr].task.name)
            //   var result = data.page.list[varr].task.name.indexOf("异常");
            //     if (result >= 0) {
            //       myArray[varr] = data.page.list[varr]
            //     }
            // }
            this.dataList = data.page.list
            this.total = data.page.count
            this.loading = false
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageNo = 1
        this.refreshList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageNo = val
        this.refreshList()
      },
         // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      claim (row) {
        this.$http.post('/flowable/task/claim', {taskId: row.task.id}).then(({data}) => {
          if (data.success) {
            this.$message.success(data.msg)
            this.refreshList()
          }
        })
      },
      unclaim (row) {
        this.$http.post('/flowable/task/unclaim', {taskId: row.task.id}).then(({data}) => {
          if (data.success) {
            this.$message.success(data.msg)
            this.refreshList()
          }
        })
      },
      todo (row, outPutForm, inits) {
        if (inits === undefined) {
          inits = ''
          console.log(inits)
        }
        console.log(inits)
        if (outPutForm === undefined) {
          outPutForm = ''
        }
        console.log(row)
        this.$http.get('/flowable/task/getTaskDef', {params: {
          taskId: row.task.id,
          taskName: row.task.name,
          taskDefKey: row.task.taskDefinitionKey,
          procInsId: row.task.processInstanceId,
          procDefId: row.task.processDefinitionId,
          status: row.status
        }}).then(({data}) => {
          if (data.success) {
            console.log(data)
            this.$router.push({
              path: './TaskForm',
              query: {formTitle: `${row.vars.title}`, outPutForm: outPutForm, inits: inits, title: `审批【${row.task.name || ''}】`, ...pick(data.flow, 'formType', 'formReadOnly', 'formUrl', 'procDefKey', 'taskDefKey', 'procInsId', 'procDefId', 'taskId', 'status', 'title', 'businessId')}
            })
            console.log(data)
          }
        })
      },
      // 新增
      inpPutLibrary (data) {
        this.loading = 'true'
        this.$refs.ProcessList.start(this.row)
        this.loading = 'false'
        // this.$refs.checkInclinationForm.init('add', '')
        // this.$router.push('/flowable/task/TaskForm?procDefId=Process_1605429711774%3A1%3Acfcd1ce4-271e-11eb-85cf-98fa9b97b99b&procDefKey=Process_1605429711774&status=start&title=发起流程【异常流程】&formType=2&formUrl=%2Fcheck%2Fcheckstandardabnormal%2FCheckStandardAbnormalForm&formTitle=异常流程&outPutForm=')
      },
      trace (row) {
        this.processInstanceId = row.task.processInstanceId
        this.visible = true
        this.$nextTick(() => {
          this.$refs.preview.init()
        })
      },
      transferTask (row) {
        this.currentTask = row.task
        this.$refs.userSelect.init()
      },
      selectUsersToTransferTask (user) {
        this.$http.post('/flowable/task/delegate', {taskId: this.currentTask.id, userId: user[0].id}).then(({data}) => {
          this.$message.success(data.msg)
          this.refreshList()
        })
      },
      resetSearch () {
        this.searchDates = ''
        this.$refs.searchForm.resetFields()
        this.$nextTick(() => {
          this.refreshList()
        })
      }
    }
  }
</script>
<style>
  .el-table .noshow {
    display: none;
  }

  .el-table .yesshow {
  }
</style>
